package soen6441;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

class RegexExample {
    public static final String EMAIL_ADDRESS
            = "tj_taill36@encs.concordia.ca";

    public static void main(String[] args) {
        // match parts of an email address
        // step 1 compile pattern
        Pattern emailPattern = Pattern.compile("(.*)@(.*)");
        // step 2 build the matcher
        Matcher emailAddress = emailPattern.matcher(EMAIL_ADDRESS);
        // step 3 call find or replace
        if ( emailAddress.find() ){
            System.out.println( "user name: " + emailAddress.group(1) );
            System.out.println( "domain: " + emailAddress.group(2) );
        }

        // make a new email from an old one
        emailPattern = Pattern.compile("[^@]*");
        emailAddress = emailPattern.matcher(EMAIL_ADDRESS);
        String newAddress = emailAddress.replaceFirst("ttaillef");
        System.out.println(newAddress);
    }
}
